package com.chixing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.commons.ServerResponse;
import com.chixing.mapper.CompanyManageMapper;
import com.chixing.pojo.ChatHistory;
import com.chixing.pojo.CompanyManage;
import com.chixing.service.IChatHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;

/**
 * 聊天控制类
 */
@RestController
public class ChatHistoryController {

    @Autowired
    private IChatHistoryService chatHistoryService;

    @Autowired
    private CompanyManageMapper companyManageMapper;

    @RequestMapping("chatHistory/getChatContent")
    public ServerResponse getChatContent(ChatHistory chatHistory) {
        String chatContent = chatHistoryService.getChatContent(chatHistory);
        QueryWrapper<CompanyManage> wrapper = new QueryWrapper<>();
        wrapper.eq("manage_name", chatHistory.getRecipientName());
        CompanyManage companyManage = companyManageMapper.selectOne(wrapper);
        if (companyManage != null)
            return ServerResponse.success(companyManage.getText1(), chatContent);
        return ServerResponse.success("暂无邮箱", chatContent);
    }

    @PostMapping("chatHistory/updateChatContent")
    public ServerResponse updateChatContent(ChatHistory chatHistory) {
        boolean b = chatHistoryService.updateChatContent(chatHistory);
        if (b) return ServerResponse.success("成功", b);
        return ServerResponse.fail("失败", b);
    }

    //获得聊天的状态，已读、未读
    @PostMapping("chatHistory/getChatStatus")
    public boolean getChatStatus(ChatHistory chatHistory) {
        Boolean status = chatHistoryService.getChatStatus(chatHistory);
        return status;
    }

    //更新小红点，返回未读消息的数量
    @GetMapping("chatHistory/getChatStatusNumber")
    public Integer getChatStatusNumber(String username) {
        Integer statusNumber = chatHistoryService.getChatStatusNumber(username);
        return statusNumber;
    }

    //利用modelAndView，定位到header.jsp页面并返回小红点图片地址
    @GetMapping("chatHistory/showImg")
    public String showImg(HttpSession session) {
        String imgUrl = "/link/static/img/redPoint.png";
        session.setAttribute("redMiddleUrl",imgUrl);
        return "ok";
    }

    //利用modelAndView，定位到header.jsp页面并返回null,不展示小红点
    @GetMapping("chatHistory/hideImg")
    public String hideImg(HttpSession session) {
        session.setAttribute("redMiddleUrl","");
        return "ok";
    }
}
